
import asyncio
import logging

from graph_agent.self.graph import create_agent

# 设置日志配置
logger = logging.getLogger(__name__)


def print_event_value(value: dict) -> None:
    """
    打印事件中的消息内容，包含安全访问判断
    """
    messages = value.get("messages")
    if isinstance(messages, list) and len(messages) > 0:
        last_message = messages[-1]
        logger.info("Assistant: %s", last_message)
    else:
        logger.warning("Invalid or missing 'messages' in event value: %s", value)

    logger.info("Test: %s", value)


async def test():
    agent = await create_agent()
    try:
        async for chunk in agent.astream(input={"messages": ["广州天气？"]},    stream_mode="updates"  # 可选values/messages
):
            for value in chunk.values():
                print('---------------------------------------')
                print_event_value(value)
            # print('---------------------------------------')
            # print(chunk)  # 处理增量输出
    except Exception as e:
        logger.error("Error during streaming: %s", e)


# 正确地运行异步函数
if __name__ == "__main__":
    asyncio.run(test())